import type { Job } from "@/pages/tool/job/entity";
import apiClient from "../../apiClient";
import type { Result } from "#/api";

export interface PageListReq {
  //任务名称
  jobName?: React.Key;
  //任务组名
  jobGroup?: React.Key;
  //当前页码
  page: number;
  //每页数量
  pageSize: number;
}

export interface PageListRes {
  //总记录数
  total: number;
  //数据列表
  records: Job[];
}

const pageList = (data: PageListReq): Promise<PageListRes> =>
  apiClient.post({
    url: "/tool/job/pageList",
    data
});

const getInfo = (id: React.Key): Promise<Job> =>
  apiClient.get({
    url: `/tool/job/${id}`
});

const add = (data: Job): Promise<Job> =>
  apiClient.post({
    url: "/tool/job",
    data
});

const update = (data: Job): Promise<Job> =>
  apiClient.put({
    url: "/tool/job",
    data
});

const remove = (ids: React.Key[]): Promise<void> =>
  apiClient.delete({
    url: "/tool/job",
    params: {
    ids: ids.join(",")
  }
});

//执行定时任务
const runJob = (data: Job): Promise<Result> => 
  apiClient.post({
    url: "/tool/job/run",
    data
  });
 
const changeStatus = (data: Job): Promise<Result> => 
  apiClient.post({
    url: "/tool/job/status/change",
    data
  });

export const jobService = {
  pageList,
  getInfo,
  add,
  update,
  remove,
  runJob,
  changeStatus
};

export default jobService;